/*
 * Jenny Ruppen 09-730-391
 * Sebastien Goldberg 08-914-855
 * Henry Raymond 08-928-061
 * Stefan Mussato 09-752-270
 */
 
package ch.uzh.ifi.sopra.gruppe9.game;

import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;



/**
 * Server-side class for the action location "bank", where you can get money during the game seasons.
 * Implements IActionLocation.
 */
@SuppressWarnings("serial")
public class Bank extends UnicastRemoteObject implements IRmiActionLocation {

	
	/**
	 * Game board
	 */
	private IGameBoard gameBoard;
	
	
	/**
	 * Array of the slots where buying agent cards can be placed. 
	 * (0 = no card, 1 = one card in the color of the color constraint, 2 = two cards)
	 * (The "+5" slot will just be marked with "1" as soon as there is at least one card on the slot.)
	 */
	private int[] buyingAgentSlots;
	
	
	/**
	 * Color constraint in the bank.
	 */
	private BuyingAgent.Color colorConstraint = BuyingAgent.Color.NONE;
	
	
	/**
	 * Constructor
	 */
	public Bank(IGameBoard gameBoard) throws RemoteException {
		super();
		
		this.gameBoard = gameBoard;

		buyingAgentSlots = new int[4];

		for(int i = 0; i < buyingAgentSlots.length; i++) {
			buyingAgentSlots[i] = 0;
		}
	}

	
	/**
	 * Returns array of the slots where buying agent cards can be placed. 
	 * (0 = no card, 1 = one card in the color of the color constraint, 2 = two cards)
	 * (The "+5" slot will just be marked with "1" as soon as there is at least one card on the slot.)
	 */
	@Override
	public int[] getBuyingAgentSlots() throws RemoteException {
		return buyingAgentSlots;
	}

	
	/**
	 * Returns id of color of color constraint. (see enumToIntTranslator in class BuyingAgent)
	 */
	@Override
	public int getColorConstraint() throws RemoteException {
		return BuyingAgent.enumToInt(colorConstraint);
	}

	
	/**
	 * Gets money of slot with index (parameter1) for player (playerIndex) from bank
	 * Only one buying agent card is used: buyingAgentID2 = -1 
	 */
	@Override
	public boolean doAction(int playerIndex, int buyingAgentID1, int buyingAgentID2, int parameter1) throws RemoteException {
		
		IPlayer player = gameBoard.getPlayer(playerIndex);
		
		if(parameter1 < 3) { // "+12", "+10", "+8"
			
			if(buyingAgentSlots[parameter1] != 0) { // Check if buying agent card slot is free.
				System.err.println("Error: Chosen buying agent slot is not free.");
				return false;
			}
			
			if(colorConstraint != BuyingAgent.Color.NONE) { // Color constraint
				if(new BuyingAgent(buyingAgentID1).getColor() != colorConstraint) {
					System.err.println("Error: Color of buying agent card does not comply with color constraint.");
					return false;
				}
			} else {
				colorConstraint = new BuyingAgent(buyingAgentID1).getColor(); // Set color constraint
			}
		}
		
		if(buyingAgentID2 != -1) { // Two buying agent card
			System.err.println("Error: Cannot give two cards at the palace of the caliph.");
			return false;
		}
		
		if(!player.removeBuyingAgent(buyingAgentID1)) {
			System.err.println("Error: Player doesn't have given buying agent card. (Buying agent ID: " + buyingAgentID1 + ")");
			return false;
		}
			
		// Put one buying agent cards on the chosen slot
		buyingAgentSlots[parameter1] = 1;
			
		// Add money to player's money balance
		int[] moneyValues = {12, 10, 8, 5};
		player.recieveMoney(moneyValues[parameter1]);
		
		gameBoard.reportUpdateMoneyBuyingAgentsInventory();
		
		// It's the next player's turn
		gameBoard.nextPlayersTurn(1, parameter1);
		
		return true;
	}
	
	
	/**
	 * Empties all buying agent slots.
	 */
	public void emptyBuyingAgentSlots() {
		colorConstraint = BuyingAgent.Color.NONE;
		for(int i = 0; i < buyingAgentSlots.length; i++) {
			buyingAgentSlots[i] = 0;
		}
	}
	
	
	/**
	 * Unused
	 */
	@Override
	public int[] getTowerElementSelectionIDs() throws RemoteException {
		return null;
	}
	
	/**
	 * Unused
	 */
	@Override
	public int[] getTowerElementPileIDs() throws RemoteException {
		return null;
	}
	
	/**
	 * Unused
	 */
	@Override
	public boolean doAction(int playerIndex, int buyingAgentID1, int buyingAgentID2) throws RemoteException {
		return false;
	}

	/**
	 * Unused
	 */
	@Override
	public boolean doAction(int playerIndex, int buyingAgentID1, int buyingAgentID2, int parameter1, int parameter2) throws RemoteException {
		return false;
	}

	/**
	 * Unused
	 */
	@Override
	public boolean doAction(int playerIndex, int buyingAgentID1, int buyingAgentID2, int parameter1, String parameter2) throws RemoteException {
		return false;
	}

}
